package com.fjec.market.mapper.sqlbuilder;

import java.util.List;

public class FarmerSqlBuilder {

    public String getFarmersByFarmerIdsAndMarkCode(int markCode, List<Long> farmerIds) {
        StringBuilder sb = new StringBuilder();
        if (markCode == 0) {
            sb.append("select * from info_farmer where CompHeadPic is not null and DepoStatus in (0, 1) ");
            if (farmerIds.size() != 0) {
                sb.append(" and Id in (");
                for (Long id : farmerIds) sb.append(id + ",");
                sb.deleteCharAt(sb.length() - 1);
                sb.append(" )");

            }
            sb.append(" order by rand() ");

        } else {
            sb.append("select farmer.* from info_farmer farmer, biz_market_busi bmb, info_market market where CompHeadPic is not null and DepoStatus in (0, 1) ");
            sb.append(" and farmer.Id=bmb.BusiId and bmb.MarkCode=market.MarkCode and market.MarkCode=").append(markCode);

            if (farmerIds.size() != 0) {
                sb.append(" and farmer.Id in ( ");
                for (Long id : farmerIds) sb.append(id).append(",");
                sb.deleteCharAt(sb.length() - 1);
                sb.append(") ");
            }

            sb.append(" order by rand() ");
//            sb.append(" and farmer.Id >= (select ceil(rand()*(select MAX(Id) from info_farmer))) ");
        }

        return sb.toString();
    }
}
